import { Dialog } from "react-vant"

interface DataType {
    method: "GET" | "POST" | "PUT" | "DELETE",
    headers?: {
        "Content-Type": string
    }
    body?: BodyInit | null
}
const fetchApi = (url: string, data: DataType = { method: "GET" }) => {
    return new Promise((resolve, reject) => {
        fetch(url, data).then((res) => res.json()).then(({ data, code }) => {
            resolve({ data, code })
        }).catch((err) => {
            Dialog.alert({
                message: '未知错误,请联系管理员',
                confirmButtonColor: '#ee0a24',
            })
            reject(err)
        })
    })
}
export default fetchApi